/**
 * Copyright (c) 2006 eMundo GmbH. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * Project: agil-client-web
 * File: GwtGenericParameterPanel.java
 *
 */

package eu.emundo.agilpro.web.client.generic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import eu.emundo.agilpro.web.client.util.StringUtil;

/**
 * Generates a parameter-panel
 * 
 * @author Fabian Wohlschl\u00E4ger
 */
public class GwtGenericParameterPanel extends VerticalPanel {
	ArrayList mWidgetList = new ArrayList();

	/**
	 * Constructor
	 */
	public GwtGenericParameterPanel() {
		super();
		
		this.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		this.setVerticalAlignment(VerticalPanel.ALIGN_TOP);
		this.setStyleName("GwtGenericParameterPanel");
	}

	/**
	 * Generates the ParameterPanel out of the parameters
	 * 
	 * @param pParams
	 *            Parameter-Hashmap, should contain a String as key and an
	 *            String[] as value
	 */
	public void setParameters(HashMap pParams) {
		// Clear old table and list of widgets
		this.clear();		
		mWidgetList.clear();
		
		if (pParams == null) {
			return;
		}

		// Generate table
		FlexTable lParamsTable = new FlexTable();
		lParamsTable.setWidth("100%");
		lParamsTable.setCellPadding(0);
		lParamsTable.setCellSpacing(0);
		lParamsTable.setStyleName("GwtGenericParameterPanel_lParamsTable");

		// Generate widgets
		Set lParamKeySet = pParams.keySet();
		Object[] lParamKeys = lParamKeySet.toArray();

		for (int lI = 0, lIMax = lParamKeys.length; lI < lIMax; lI++) {
			// Parameter-Label
			TextBox lParamLabel = new TextBox();
			lParamLabel.setEnabled(false);
			lParamLabel.setText(lParamKeys[lI].toString());
			lParamLabel.setStyleName("GwtGenericParameterPanel_lParamLabel");

			mWidgetList.add(lParamLabel);
			
			// Parameter-Value
			String[] lParamValue = (String[]) pParams.get(lParamKeys[lI].toString());
			
			TextBox lParamTextBox = new TextBox();
			lParamTextBox.setStyleName("GwtGenericParameterPanel_lParamTextBox");

			StringBuffer lParamText = new StringBuffer();
			// If the parameter should not be displayed as a combobox
			if (lParamValue.length == 1) {
				String lValue = lParamValue[0];
				
				// If the value must be replaced with another value
				if(lValue.startsWith("$"))	{
					String lKey = lValue.substring(1, lValue.length()).toLowerCase();
					
					for (int lI2 = 0, lIMax2 = lParamKeys.length; lI < lIMax2; lI2++) {	
						if(lParamKeys[lI2].toString().toLowerCase().equals(lKey))	{
							lParamValue = (String[]) pParams.get(lParamKeys[lI2].toString());
							lValue = lParamValue[0];
							break;
						}
					}
				}
				
				lParamText.append(lValue);
			} else {
				for (int lJ = 0, lJMax = lParamValue.length; lJ < lJMax; lJ++) {
					if (lJ != 0) {
						lParamText.append(" | " + "'" + lParamValue[lJ] + "'");
					} else {
						lParamText.append("'" + lParamValue[lJ] + "'");
					}
				}
			}

			lParamTextBox.setText(lParamText.toString());

			mWidgetList.add(lParamTextBox);
		}
		
		// Add Widgets
		int lCol = 0;
		int lRow = 0;
		Iterator lWidgetIt = mWidgetList.iterator();

		while (lWidgetIt.hasNext()) {
			if (lCol == 2) {
				lCol = 0;
				lRow++;
			}

			lParamsTable.setWidget(lRow, lCol, (Widget) lWidgetIt.next());

			lCol++;
		}

		this.add(lParamsTable);
		this.setCellWidth(lParamsTable, "100%");
		this.setCellHeight(lParamsTable, "100%");
	}

	/**
	 * Gets the parameter which where inputed in the ParameterPanel
	 * 
	 * @return Parameter-Hashmap, which contains a String as key and an String[]
	 *         as value
	 */
	public HashMap getParameters() {
		HashMap lParams = new HashMap();

		Iterator lWidgetIt = mWidgetList.iterator();

		while (lWidgetIt.hasNext()) {
			// Getting parameter-key
			TextBox lParamKeyLabel = (TextBox) lWidgetIt.next();
			String lParamKey = lParamKeyLabel.getText();
			
			// Getting parameter-value
			TextBox lParamValueTextBox = (TextBox) lWidgetIt.next();
			String lTmpParamValue = lParamValueTextBox.getText();
			String[] lParamValue = null;

			// If the data was displayed in a combobox
			if (lTmpParamValue.matches("^'[^']+'( \\| '[^']+')*$")) {
				// Replace ' with nothing
				lTmpParamValue = StringUtil.replaceAll(lTmpParamValue, "'", "");

				// Split
				lParamValue = StringUtil.split(lTmpParamValue, " | ");
			} else {
				lParamValue = new String[1];
				lParamValue[0] = lTmpParamValue;
			}
			
			lParams.put(lParamKey, lParamValue);
		}

		return lParams;
	}

	/**
	 * Used for controlling the enable/disable the ParameterPanel
	 * 
	 * @param pEnabled
	 *            False if the user shouldn't input data in the widgets
	 */
	public void setEnabled(boolean pEnabled) {
		Iterator lWidgetIt = mWidgetList.iterator();

		// Enable all widgets
		while (lWidgetIt.hasNext()) {
			Widget lWidget = (Widget) lWidgetIt.next();

			if (lWidget instanceof TextBox) {
				TextBox lTextbox = (TextBox) lWidget;
				lTextbox.setEnabled(pEnabled);
			}
		}
	}
}